package com.sct.myopenglapp;

import com.sct.myopenglapp.R;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import  android.app.Activity;
import  android.opengl.GLSurfaceView;
import  android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import  android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;

import  book.BouncySquare.*;
import book.BouncyCube.*;


import com.sct.myopenglapp.PlayerJni;
import com.sct.screen.Screen;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    static {
        //System.loadLibrary("gnustl_shared");
        System.loadLibrary("sct");
        System.loadLibrary("rtsp");
        System.loadLibrary("codec");
        System.loadLibrary("playerjni");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        GLSurfaceView view = new GLSurfaceView(this);
//        view.setRenderer(new SquareRenderer(false, this.getApplicationContext()));
        view.setRenderer(new Screen(0));
//        view.setRenderer(new CubeRenderer(true));

        rGLView = (RelativeLayout) findViewById(R.id.glView);
        rGLView.addView(view);

        //获取屏幕的尺寸
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width  = dm.widthPixels;//宽度height = dm.heightPixels ;//高度
        int height = dm.heightPixels;
        _landscapeParams = new RelativeLayout.LayoutParams(height, height*9/16);
        _landscapeParams.addRule(RelativeLayout.CENTER_IN_PARENT);

        _portraitParams = new RelativeLayout.LayoutParams(width, width *9 / 16);
        _portraitParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        _portraitParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        _portraitParams.setMargins(0, 0, 0, 0);
        rGLView.setLayoutParams(_portraitParams);

        _btnShow  = (Button) findViewById(R.id.showButton);
        _btnShow.setOnClickListener(this);
        _btnClose = (Button) findViewById(R.id.closeButton);
        _btnClose.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.showButton:
                Log.v(TAG, String.format("%s:%d", "show click", 100));
                PlayerJni.show();
                break;

            case R.id.closeButton:
                Log.v(TAG, String.format("%s:%d", "close click", 2));
                PlayerJni.close();
                break;

            default:
                break;
        }
    }

    private RelativeLayout rGLView;
    private Button _btnShow;
    private Button _btnClose;

    private RelativeLayout.LayoutParams _landscapeParams;//横屏参数
    private RelativeLayout.LayoutParams _portraitParams;//竖屏参数

    private final String TAG = "app";
}
